'use strict';
const { promises } = require('nodemailer/lib/xoauth2');
const fs = require("fs")
const path = require("path");
const { format } = require('path');
const Controller = require('egg').Controller;

class PersonalController extends Controller {
    async personal() {
        const { ctx } = this;
        let email = ctx.session.email;
        console.log(email);
        let sql_personal = `select * from user where email='${email}'`
        let res_personal = await this.ctx.app.mysql.query(sql_personal)
        if (res_personal[0]) {
            ctx.body = { code: 201, res_personal };
        } else {
            ctx.body = { code: 401, info: "notfound" };
        }
    }
    async changePersonal() {
        const { ctx } = this;
        let ziduan = ctx.request.body
        let f1 = ctx.request.files;
        let email = ctx.session.email;
        if (f1[0]) {
            let oldpath = f1[0].filepath
            let newpath = __dirname + "/../public/img/avatar/" + path.basename(oldpath)
            fs.renameSync(oldpath, newpath)
            ziduan[f1[0].field] = "/public/img/avatar/" + path.basename(oldpath)
        } else {
            ziduan['avatar'] = ''
        }
        if (ziduan.username != '') {
            let sql = `update user set username='${ziduan.username}' where email='${email}'`
            let res = await ctx.app.mysql.query(sql)
        }
        if (ziduan.avatar != '') {
            let sql = `update user set avatar='${ziduan.avatar}' where email='${email}'`
            let res = await ctx.app.mysql.query(sql)
        }
        if (ziduan.telephone != '') {
            let sql = `update user set telephone=${ziduan.telephone} where email='${email}'`
            let res = await ctx.app.mysql.query(sql)
        }
        if (ziduan.QQ != '') {
            let sql = `update user set QQ=${ziduan.QQ} where email='${email}'`
            let res = await ctx.app.mysql.query(sql)
        }
        ctx.body = { code: 202, info: '修改成功' };
    }
    async changePassword() {
        const { ctx } = this;
        let ziduan = ctx.request.body
        let email = ctx.session.email;
        let sql = `update user set password='${ziduan.password}' where email='${email}'`
        let res = await ctx.app.mysql.query(sql)
        ctx.body = { code: 203, info: '修改成功' }
    }
    async myBook() {
        const { ctx } = this;
        let email = ctx.session.email;
        let sql_book_id = `select book_id from follow_books where user_email='${email}'`
        let res_book_id = await ctx.app.mysql.query(sql_book_id)
        let bookData = []
        function dataFormat(datestr) {
            let time = new Date(datestr).getTime();
            const date = new Date(time);
            const Y = date.getFullYear() + "-";
            const M =
                (date.getMonth() + 1 < 10
                    ? "0" + (date.getMonth() + 1)
                    : date.getMonth() + 1) + "-";
            const D =
                (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
            const h =
                (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
            const m =
                (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
                ":";
            const s =
                date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
            const dateString = Y + M + D + h + m + s;
            return dateString;
        }
        for (let i = 0; i < res_book_id.length; i++) {
            let sql_book = `select bookID,name1,cover from book where bookID=${res_book_id[i].book_id}`
            let res_book = await ctx.app.mysql.query(sql_book)
            let sql_chapter = `select zhangjie,chapterName,chapterTime from chapter where bookID=${res_book_id[i].book_id}`
            let res_chapter = await ctx.app.mysql.query(sql_chapter)
            res_chapter[0].chapterTime = dataFormat(res_chapter[0].chapterTime)
            res_book[0].chapter = res_chapter[0]
            bookData.push(res_book[0])
        }
        ctx.body = { code: 204, bookData }
    }
    async unfollow() {
        const { ctx } = this;
        let email = ctx.session.email;
        let sql_select = `select * from follow_books where book_id=${ctx.query.bookID} and user_email='${email}'`
        let res_select = await ctx.app.mysql.query(sql_select)
        if (res_select[0]) {
            let sql = `delete from follow_books where book_id=${ctx.query.bookID} and user_email='${email}'`
            let res = await ctx.app.mysql.query(sql)
        } else {
            let sql = `insert into follow_books (book_id,user_email) values (${ctx.query.bookID},'${email}')`
            let res = await ctx.app.mysql.query(sql)
        }
        ctx.body = { code: 205 }
    }
    async myComment() {
        const { ctx } = this;
        let email = ctx.session.email;
        let sql_comment = `select * from comment where user_email='${email}'`
        let res_comment = await ctx.app.mysql.query(sql_comment)
        let commentData = []
        let data = []
        for (let i = 0; i < res_comment.length; i++) {
            let sql_book = `select name1 from book where bookID=${res_comment[i].book_id}`
            let res_book = await ctx.app.mysql.query(sql_book)
            data.push({ 'bookID': res_comment[i].book_id, 'bookname': res_book[0].name1, 'content': res_comment[i].content })
        }
        commentData.push(data)
        ctx.body = { code: 206, commentData }
    }
}

module.exports = PersonalController;
